package com.stec.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stec.components.web.controller.BaseController;
import com.stec.framework.form.JsonRequestBody;
import com.stec.framework.metadata.bean.Id;
import com.stec.framework.metadata.bean.ResultForm;
import com.stec.framework.metadata.usage.page.PageForm;
import com.stec.masterdata.dto.TeacherList;
import com.stec.masterdata.entity.Teacher;
import com.stec.masterdata.handler.TeacherHandler;
import com.stec.utils.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jodd.util.ObjectUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.text.html.parser.Entity;
import java.util.List;

@Api(tags="教师单表管理")
@RestController
@RequestMapping(value="/rest/teacher",method= RequestMethod.POST)
public class TeacherRestController extends BaseController {
    @Reference
    private TeacherHandler teacherHandler;

    @ApiOperation("保存一条记录")
    @RequestMapping("/save")
    public ResultForm<Teacher> save(@RequestBody Teacher requestBody){
        //logger("保存一条记录",requestBody);
        return ResultForm.createSuccessResultForm(teacherHandler.save(requestBody),"保存成功");
    }

    @ApiOperation("通过主键获取一条记录")
    @RequestMapping("/get")
    public ResultForm<Teacher> get(@RequestBody Id requestBody){
        Long id=requestBody.getId();
        if (ObjectUtils.isNull(requestBody.getId())){
            return ResultForm.createErrorResultForm(null,"ID不能为空！");
        }
        return ResultForm.createSuccessResultForm(teacherHandler.selectByPrimaryKey(id),"查询成功");
    }

    @ApiOperation("通过主键删除一条记录")
    @RequestMapping("/delete")
    public ResultForm delete(@RequestBody Id requestBody){
        Long id=requestBody.getId();
        if (ObjectUtils.isNull(requestBody.getId())) {
            return ResultForm.createErrorResultForm(null, "ID不能为空！");
        }
        return ResultForm.createSuccessResultForm(teacherHandler.deleteByPrimaryKey(id),"删除成功");
    }

    @ApiOperation("分页查询列表")
    @RequestMapping("/list")
    public ResultForm list(@ApiParam(value = "")@RequestBody JsonRequestBody jsonRequestBody){
        PageForm pageForm=jsonRequestBody.getPageForm();
        Teacher entity=jsonRequestBody.tryGet(Teacher.class);
        EntityWrapper<Teacher> wrapper=new EntityWrapper<>(entity);
//        String name=jsonRequestBody.getString("name");
//        wrapper.like("name",name);
        return ResultForm.createSuccessResultForm(teacherHandler.selectEntities(wrapper,pageForm));
    }

    @ApiOperation("EntityWrapper使用测试 JsonRequestBody作为输入")
    @RequestMapping("/entityWrapper")
    public ResultForm entity(@ApiParam(value = "")@RequestBody JsonRequestBody jsonRequestBody){
        String name=jsonRequestBody.getString("name");
        EntityWrapper<Teacher> wrapper=new EntityWrapper<>();
        wrapper.eq("name",name);
        return ResultForm.createSuccessResultForm(teacherHandler.selectEntities(wrapper));
    }

    @ApiOperation("EntityWrapper使用测试 Teacher作为输入")
    @RequestMapping("/entityWrapper0")
    public ResultForm entity0(@RequestBody Teacher requestBody){
        String name=requestBody.getName();
        EntityWrapper<Teacher> wrapper=new EntityWrapper<>();
        //wrapper.eq("name",name);
        //wrapper.setSqlSelect("id","name","state");
        //return ResultForm.createSuccessResultForm(teacherHandler.selectByName(name));
        return ResultForm.createSuccessResultForm(teacherHandler.selectEntities(wrapper));
    }

    @ApiOperation("Dto输入ID列表 查询各个教师信息")
    @RequestMapping("/dto0")
    public ResultForm dto0(@RequestBody TeacherList teacherList){
        Integer a=teacherList.getA();
        Integer b=teacherList.getB();
        List<Integer> list=teacherList.getTeacherList();
        EntityWrapper<Teacher> wrapper=new EntityWrapper<>();
        wrapper.between(Teacher.COLUMN_STATE,a,b).orderBy(Teacher.COLUMN_STATE,false).orderBy(Teacher.COLUMN_NAME,false);
        //wrapper.in(Teacher.COLUMN_ID,list);
        //wrapper.eq("name",name);
        //wrapper.setSqlSelect("id","name","state");
        //return ResultForm.createSuccessResultForm(teacherHandler.selectByName(name));
        return ResultForm.createSuccessResultForm(teacherHandler.selectEntities(wrapper));
    }
}
